
#include "User.h"



//#include<QWidget>
//#include<QDialog>

//#include <QApplication>
//#include<QTextCodec>
//#include<QLabel>
string PATH;
vector<Email> GetallEmail(int argc);

int main22(int argc, char *argv[])
{
//    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
//
//    return a.exec();em

    char buf[100];
    GetModuleFileNameA(NULL, buf, 100);
    string t = string(buf);
    string PATH = t.substr(0, t.rfind('\\'));
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }
    //绑定用户
    if (argc == 4 && !strcmp(argv[1], "bind")) {
        try {
            POP3Connector pop3(argv[2], argv[3]);
            SaveInfoToFile(argv[2], argv[3]);
            printf("binding success!\n");
        }
        catch (Exception e) {
            e.Print();
        }
    }
    else if (argc == 3 && !strcmp(argv[2], "status")) {
        string addr;
        string pass;
        if (GetInfoFromFile(addr, pass)) {
            printf("Eamil Address: %s\nPassword: %s\n", addr.c_str(), pass.c_str());
        }
        else printf("you did not bind before\n");
    }
    // 查看精简
    else if ((argc == 2 || argc == 3) && !strcmp(argv[1], "list")) {
        try {
            string addr;
            string pass;
            if (GetInfoFromFile(addr, pass)) {
                POP3Connector pop3(addr, pass);
                if (argc == 2) pop3.List();
                else {
                    int t;
                    if (isNumber(argv[2])) {
                        sscanf(argv[2], "%d", &t);
                        pop3.List(t);
                    }
                    else printf("please input a number(>-1) after list\n");
                }
            }
            else printf("you did not bind before\n");
        }
        catch (Exception e) {
            e.Print();
        }
    }
    //接受
    else if (argc == 3 && !strcmp(argv[1], "retr")) {
        try {
            string addr;
            string pass;
            if (GetInfoFromFile(addr, pass)) {
                POP3Connector pop3(addr, pass);
                int id = 0;
                if (isNumber(argv[2])) {
                    sscanf(argv[2], "%d", &id);
                    //pop3.Retr(id);
                }
                else printf("please input a number(>-1) after retr\n");
            }
            else printf("you did not bind before\n");
        }
        catch (Exception e) {
            e.Print();
        }
    }
    //发送
    else if ((argc == 4 || argc == 5 || argc == 6) && !strcmp(argv[1], "send")) {
        /**
         * 1 send            2
         * 2 email address   3
         * 3 contentfilename
         * 4 utf-8           4
         * 5 attachname      5
         */
        try {
            string addr="992115865@qq.com";
            string pass;
            //myemail send username@xxxx.xx contentfilename [utf-8] [attachmentfilename]
            if (GetInfoFromFile(addr, pass)) {

                string charset = "gb18030";
                MIMEContent cont("multipart/alternative;", "8Bit");
                if (argc == 4) {
                    cont.AddMIMEContent(new MIMEContent("text/plain; charset=\"" + charset + "\";", "base64", argv[3]));
                }
                else if (argc > 4 && string(argv[4]) == "utf-8") {
                    charset = "utf-8";
                    cont.AddMIMEContent(new MIMEContent("text/plain; charset=\"" + charset + "\";", "base64", argv[3]));
                    if (argc == 6) {
                        cont.AddMIMEContent(new MIMEContent("application/octet-stream;",
                                                            "base64", argv[5], "attachment;"));
                    }
                }
                else if (argc == 5) {
                    cont.AddMIMEContent(new MIMEContent("text/plain; charset=\"" + charset + "\";", "base64", argv[3]));
                    cont.AddMIMEContent(new MIMEContent("application/octet-stream;",
                                                        "base64", argv[4], "attachment;"));
                }
                else throw Exception("only can attach one attachment");

                string content = Email().SetFrom(addr).SetTo(argv[2]).SetSubject(argv[3])
                        .SetMIMEContent(&cont)
                        .BuidEmailString();

                SMTPConnector smtp(addr, pass);
                if (!smtp.SendEmail(argv[2], content)) {
                    throw Exception("send email failed");
                }
                else printf("send success!\n");

            }
            else printf("you did not bind before\n");
        }
        catch (Exception e) {
            e.Print();
        }
    }
    else {
        printf("unable to parse command\n");
        WSACleanup();
    }
    return 0;
}


void binduser(string user_email,string password){
    char buf[100];
    GetModuleFileNameA(NULL, buf, 100);
    string t = string(buf);
    string PATH = t.substr(0, t.rfind('\\'));
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return ;
    }
    //绑定用户
    try {
        POP3Connector pop3(user_email,password );
        SaveInfoToFile(user_email,password );
        printf("binding success!\n");
    }
    catch (Exception e) {
        e.Print();
    }
    WSACleanup();

}

vector<Email> GetallEmail(int argc){

    vector<Email> allEmail;
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return allEmail;
    }
    try {
        string addr;
        string pass;
        if (GetInfoFromFile(addr, pass)) {
            POP3Connector pop3(addr, pass);
            if (argc == -1)  allEmail = pop3.List();
            else {
                // int t;

                allEmail = pop3.List(argc);

                // else printf("please input a number(>-1) after list\n");
            }
        }
        else printf("you did not bind before\n");
    }
    catch (Exception e) {
        e.Print();
    }
    WSACleanup();
    return allEmail;
}



int main (){

/// 索取邮箱测试u
   // vector<Email> ae = GetallEmail(-1);
   // for ( const Email& temp : ae) {
   //     cout<<temp.subject<<endl;
    //}
   // User myuser ("992115865@qq.com","mdxdacmrdvvhbbgb");

////////////用户注册测试
//    vector<User> myusers;
// myusers = User::getallUserfromConf();
//
//User guer("771582678@qq.com","rlvohyxyiynebfih");
//
//User guer2(  "992115865@qq.com","mdxdacmrdvvhbbgb");


/////////// 接受测试
//    WSADATA wsaData;
//    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
//    if (iResult != 0) {
//        printf("WSAStartup failed: %d\n", iResult);
//
//    }
//    vector<User> all  = User::getallUserfromConf();
// User a2 = all[0];
// User a3 = all[1];
//// vector<Email> allemail;
////
////        string addr = a3.email;
////        string pass = a3.password;
////        GetInfoFromFile(addr ,pass);
////
////        POP3Connector pop3(addr, pass);
////        pop3.Retr(1);
////    User guer("771582678@qq.com","rlvohyxyiynebfih",true);
////    User guer2(  "992115865@qq.com","mdxdacmrdvvhbbgb",true);
//
//a2.getallEmailinfo();
//
//
//    WSACleanup();

//////////////////////////send测试
//int argv =4;
//char *argc[4];
//argc[1] = (char*)"send";
//argc[2] =(char*)"992115865@qq.com";
//argc[3]= (char*)"email.cbp";
//
//    main22(argv,argc);


    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);

    }


vector<User> all  = User::getallUserfromConf();
 User a2 = all[0];
 User a3 = all[1];

// a3.getallEmailinfo();

a2.SendEmail("hello","992115865@qq.com","test.txt","email.cbp",true);

    return 0;
}